package api

import (
	"examination-management/api/handler/request"
	pb "examination-management/api/proto"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
)

func CreateTestManagement(c *gin.Context) {
	var req request.CreateTestManagementReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewExaminationClient(conn)

	management, _ := client.CreateTestManagement(c, &pb.CreateTestManagementReq{
		TestQuestionType:     req.TestQuestionType,
		Difficulty:           req.Difficulty,
		ProfessionalScopeOne: req.ProfessionalScopeOne,
		ProfessionalScopeTwo: req.ProfessionalScopeTwo,
		Author:               req.Author,
		TestQuestions:        req.TestQuestions,
	})
	c.JSON(200, management)
}

func UpdateTestManagement(c *gin.Context) {
	var req request.UpdateTestManagementReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewExaminationClient(conn)

	management, _ := client.UpdateTestManagement(c, &pb.UpdateTestManagementReq{
		TMId:                 req.Id,
		TestQuestionType:     req.TestQuestionType,
		Difficulty:           req.Difficulty,
		ProfessionalScopeOne: req.ProfessionalScopeOne,
		ProfessionalScopeTwo: req.ProfessionalScopeTwo,
		Author:               req.Author,
		TestQuestions:        req.TestQuestions,
	})
	c.JSON(200, management)
}

func CreateTestOption(c *gin.Context) {
	var req request.CreateTestOptionReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewExaminationClient(conn)

	option, _ := client.CreateTestOption(c, &pb.CreateTestOptionReq{
		TestManagementId: req.TestManagementId,
		Select:           req.Select,
		Option:           req.Option,
	})

	c.JSON(200, option)
}
func CreateTestModule(c *gin.Context) {
	var req request.CreateTestModuleReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewExaminationClient(conn)

	option, _ := client.CreateTestModule(c, &pb.CreateTestModuleReq{
		UId:               uint64(c.GetUint("userId")),
		TestManagementId:  req.TestManagementId,
		FounderId:         req.FounderId,
		TestOptionId:      req.TestOptionId,
		TestManagementIds: req.TestManagementIds,
		TestAnswer:        req.TestAnswer,
		Analysis:          req.Analysis,
		Keywords:          req.Keywords,
		ViewingRange:      req.ViewingRange,
	})

	c.JSON(200, option)
}
func TestModuleInfo(c *gin.Context) {
	var req request.TestModuleInfoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewExaminationClient(conn)

	option, _ := client.TestModuleInfo(c, &pb.TestModuleInfoReq{
		UId: uint64(c.GetUint("userId")),
		Id:  req.Id,
	})
	c.JSON(200, option)
}
